
import matplotlib.pyplot as plt
import numpy as np

# 设置中文显示
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False

# 数据准备
days = [
    "05/23", "05/24", "05/25", "05/26", "05/27",
    "05/28", "05/29", "05/30", "05/31", "06/01",
    "06/02", "06/03", "06/04", "06/05", "06/06"
]
min_temps = [22, 23, 24, 25, 24, 25, 24, 23, 22, 23, 24, 25, 24, 23, 22]
max_temps = [29, 30, 31, 32, 33, 34, 31, 30, 29, 30, 32, 33, 31, 30, 29]
precipitation = [3.2, 5.1, 0.0, 0.5, 8.7, 15.4, 12.3, 2.1, 0.0, 1.2, 6.8, 20.1, 7.5, 3.0, 0.2]

# 创建图表和主坐标轴
fig, ax1 = plt.subplots(figsize=(12, 6))

# 绘制温度折线图（左轴）
ax1.plot(days, min_temps, 'b-o', label='最低温')
ax1.plot(days, max_temps, 'r-*', label='最高温')
ax1.set_xlabel('日期（天）', fontsize=12)
ax1.set_ylabel('温度 (°C)', color='k', fontsize=12)
ax1.tick_params(axis='y', labelcolor='k')
ax1.set_xticks(days)
ax1.grid(axis='x', linestyle='--', alpha=0.5)

# 创建次坐标轴（右轴）
ax2 = ax1.twinx()
bars = ax2.bar(days, precipitation, color='#1E90FF', width=0.6, alpha=0.7, label='降水量')

# 添加降水量数据标签
for bar in bars:
    height = bar.get_height()
    ax2.text(bar.get_x() + bar.get_width()/2., height,
             f'{height:.1f}',
             ha='center', va='bottom')

ax2.set_ylabel('降水量 (mm)', color='#1E90FF', fontsize=12)
ax2.tick_params(axis='y', labelcolor='#1E90FF')

# 合并图例
lines1, labels1 = ax1.get_legend_handles_labels()
lines2, labels2 = ax2.get_legend_handles_labels()
ax1.legend(lines1 + lines2, labels1 + labels2, loc='upper left')

# 设置标题
plt.title('清远市未来15天气温/降水量', fontsize=14, pad=20)
plt.tight_layout()
plt.show()
